uses crt,COROUT;
var
   proc1:descptr;
   proc2:descptr;
   proc3:descptr;
function isEscape:boolean;
var c:char;
begin
  if (keypressed) then
	begin
	   writeln('isEscape running, procedure 1');
	   c := readkey;
	   writeln(ord(c));
	   if (ord(c) = 0) then
		begin
		 c:=readkey;
		 if (c = chr(27)) then
		   begin
			isEscape :=true;
		   end
		else
		   begin
			isEscape :=false;
		   end;
		end
	    else
		begin
		   if(ord(c) = 27) then isEscape := true else
		   isEscape:=false;
		end;
	end
    else
	begin
	   isEscape:=false;
	end;
end;
procedure p1;
begin
   while true do
   begin
   if isEscape then Transfer(proc1, main) else
	begin  
	   writeln('procedure 1');
	   Transfer(proc1, proc2);
	end;
   end;
end;
procedure p2;
begin
   while true do
   begin
	   writeln('procedure 2');

	   Transfer(proc2, proc3);
	
   end;
end;
procedure p3;
begin
   while true do
   begin
	   writeln('procedure 3');
	   Transfer(proc3, proc1);
	
   end;
end;
var
   memory:longint;
begin
  memory:=MemAvail;
  NewProcess(@p1, proc1);
  NewProcess(@p2, proc2);
  NewProcess(@p3, proc3);
  writeln('new size of Memory in end:', MemAvail);
  readln;
  Transfer(main,proc1);
  writeln('main');
  writeln('size of Memory in begin:', memory);
  writeln('new size of Memory in end:', MemAvail);
  KillProcess(Proc1);
  KillProcess(Proc2);
  KillProcess(Proc3);
  writeln('new size of Memory in end:', MemAvail);
end.
